<template>
  <div class="goods-item" @click="itemClick">
    <!-- 因为传递过来的goodsItem已经是个数组了，所以这里不用写循环 -->
    <img v-lazy="showImage" alt="" @load="imageLoad" />
    <div class="goods-info">
      <p>{{ goodsItem.title }}</p>
      <span class="price">￥{{ goodsItem.price }}</span>
      <span class="collect">{{ goodsItem.cfav }}</span>
    </div>
  </div>
</template>

<script>
export default {
  name: "Goodslistitem",

  props: {
    goodsItem: {
      type: Object,
      default() {
        return {};
      },
    },
  },

  data() {
    return {};
  },

  mounted() {},

  computed: {
    showImage() {
      return (
        this.goodsItem.img || this.goodsItem.image || this.goodsItem.show.img
      );
    },
  },

  methods: {
    imageLoad() {
      //方法一： 判断当前页面是不是首页 再确定发不发送这个事件
      // if(this.$route.path.indexOf('/home')) {
      //   this.$bus.$emit('homeItemImgLoad')  //这里发送的是首页的商品数据刷新
      // } else if(this.$route.path.indexOf('/detail')) {
      //   this.$bus.$emit('detailItemImgLoad')
      // }

      //方法二： 离开home页面的时候取消掉这个发送过去的方法
      this.$bus.$emit('itemImgLoad')

      // if (this.$route.path.indexOf("/home")) {
      //   this.$bus.$emit("homeImageLoad");
      // } else if (this.$route.path.indexOf("/detail")) {
      //   this.$bus.$emit("detailImageLoad");
      // }
    },
    itemClick() {
      this.$router.push("/detail/" + this.goodsItem.iid);
    },
  },
};
</script>

<style lang="css" scoped>
.goods-item {
  padding: 5px 0 45px;
  position: relative;
  /* width: 175px; */
  width: 48%;
  /* margin: 0 5px; */
  background-color: #fff;
}

.goods-item img {
  width: 100%;
  border-radius: 5px;
}

.goods-info {
  font-size: 12px;
  position: absolute;
  bottom: 5px;
  left: 0;
  right: 0;
  overflow: hidden;
  text-align: center;
}

.goods-info p {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin-bottom: 3px;
}

.goods-info .price {
  color: var(--color-high-text);
  margin-right: 20px;
}

.goods-info .collect {
  position: relative;
}

.goods-info .collect::before {
  content: "";
  position: absolute;
  left: -15px;
  top: -0.5px;
  width: 14px;
  height: 14px;
  background: url("~assets/img/common/collect.svg") 0 0/14px 14px;
}
</style>